{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d2d947ba",
   "metadata": {},
   "source": [
    "## 理解误差反向传播法\n",
    "1. 基于数学式\n",
    "2. 基于计算图(computational graph)\n",
    "\n",
    "## 链式法则(chain rule\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c547db69",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "苹果价格: 220.00000000000003\n",
      "2.2 110.00000000000001 200\n",
      "总价: 715.0000000000001\n",
      "2.2 110.00000000000001 3.3000000000000003 165.0 650\n",
      "[[ 1.  -0.5]\n",
      " [-2.   3. ]]\n",
      "[[False  True]\n",
      " [ True False]]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 乘法层的实现\n",
    "class MulLayer:\n",
    "    def __init__(self):\n",
    "        self.x = None\n",
    "        self.y = None\n",
    "    def foreward(self,x,y):\n",
    "        self.x = x\n",
    "        self.y = y\n",
    "        out = x * y\n",
    "        return out\n",
    "    def backward(self,dout):\n",
    "        dx = dout * self.y\n",
    "        dy = dout * self.x\n",
    "        return dx,dy\n",
    "\n",
    "# 苹果价格 = 单价*数量*税率\n",
    "apple = 100\n",
    "apple_num = 2\n",
    "tax = 1.1 #消费税\n",
    "\n",
    "mul_apple_layer = MulLayer()\n",
    "mul_tax_layer = MulLayer()\n",
    "\n",
    "#前向传播\n",
    "apple_price = mul_apple_layer.foreward(apple,apple_num)\n",
    "price = mul_tax_layer.foreward(apple_price,tax)\n",
    "print(\"苹果价格:\",price)\n",
    "\n",
    "#反向传播,∂L/∂苹果单价，∂L/∂苹果个数，∂L/∂消费税\n",
    "dprice = 1\n",
    "dapple_price,dtax = mul_tax_layer.backward(dprice)\n",
    "dapple,dapple_num = mul_apple_layer.backward(dapple_price)\n",
    "print(dapple,dapple_num,dtax)\n",
    "\n",
    "# 加法层的实现\n",
    "class AddLayer:\n",
    "    def __init__(self):\n",
    "        pass\n",
    "    def foreward(self,x,y):\n",
    "        out = x + y\n",
    "        return out\n",
    "    def backward(self,dout):\n",
    "        dx = dout * 1\n",
    "        dy = dout * 1\n",
    "        return dx,dy\n",
    "    \n",
    "# 买两个苹果和三个橘子\n",
    "apple = 100\n",
    "apple_num = 2\n",
    "orange = 150\n",
    "orange_num = 3\n",
    "tax = 1.1\n",
    "\n",
    "# layer\n",
    "mul_apple_layer = MulLayer()\n",
    "mul_orange_layer = MulLayer()\n",
    "add_apple_orange_layer = AddLayer()\n",
    "mul_tax_layer = MulLayer()\n",
    "\n",
    "#前向传播\n",
    "apple_price = mul_apple_layer.foreward(apple,apple_num)\n",
    "orange_price = mul_orange_layer.foreward(orange,orange_num)\n",
    "all_price = add_apple_orange_layer.foreward(apple_price,orange_price)\n",
    "price = mul_tax_layer.foreward(all_price,tax)\n",
    "\n",
    "#反向传播\n",
    "dprice = 1\n",
    "dallprice,dtax = mul_tax_layer.backward(dprice)\n",
    "dapple_price,dorange_price = add_apple_orange_layer.backward(dallprice)\n",
    "dorange,dorange_num = mul_orange_layer.backward(dorange_price)\n",
    "dapple,dapple_num = mul_apple_layer.backward(dapple_price)\n",
    "\n",
    "print(\"总价:\",price)\n",
    "print(dapple,dapple_num,dorange,dorange_num,dtax)\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "# 激活函数ReLU(Rectified Linear Unit)\n",
    "class Relu:\n",
    "    def __init__(self):\n",
    "        self.mask = None\n",
    "    def forward(self,x):\n",
    "        self.mask = (x <= 0)\n",
    "        out = x.copy()\n",
    "        out[self.mask] = 0\n",
    "        return out\n",
    "    def backward(self,dout):\n",
    "        dout[self.mask] = 0\n",
    "        dx = dout\n",
    "        return dx\n",
    "x = np.array([[1.0,-0.5],[-2.0,3.0]])\n",
    "print(x)\n",
    "mask = (x <= 0)\n",
    "print(mask)\n",
    "\n",
    "# Sigmoid层\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "49b30cc4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
